package com.isunland.managesystem.adapter;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.Expert;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.widget.MyExpandableTextView;
import java.util.ArrayList;

/**
 * 对象功能: 资深用户专家
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/16 18:08
 */
public class MyExpertListAdapter<T> extends BaseAdapter {

  private ArrayList<T> objects = new ArrayList<>();

  private LayoutInflater layoutInflater;
  private Callback mCallback;
  private SparseBooleanArray collapsedStatus;

  public interface Callback {
    void clickView(int type, int position);
  }

  public MyExpertListAdapter(Context context, ArrayList<T> objects, Callback callback) {
    this.layoutInflater = LayoutInflater.from(context);
    this.objects = objects;
    this.mCallback = callback;
    this.collapsedStatus = new SparseBooleanArray();
  }

  @Override public int getCount() {
    return objects.size();
  }

  @Override public T getItem(int position) {
    return objects.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = layoutInflater.inflate(R.layout.adapter_expert_select, parent, false);

      convertView.setTag(new ViewHolder(convertView));
    }

    ViewHolder holder = (ViewHolder) convertView.getTag();
    final Expert item = (Expert) getItem(position);
    holder.ivLogo.setImageResource(R.drawable.photo);
    holder.tvTitle.setText(item.getMembername());

    //holder.tvContent.setText(item.getScoretypename());//分类
    holder.tvContentLeft.setText("专业：");//专家分类
    holder.llExpand.setText(item.getScoretypename(), collapsedStatus,
        position);//分类,解决ExpandableTextView被复用
    holder.tvBottomLeft.setText(MyStringUtil.connect("积分值：", item.getScoredata()));

    if (item.getRemark() != null && "T".equalsIgnoreCase(item.getRemark())) {
      holder.ivAttent.setBackgroundResource(R.drawable.dianzan_xuanzhong_icon);
    } else {
      holder.ivAttent.setBackgroundResource(R.drawable.dianzan_moren_icon);
    }

    holder.ivAttent.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        mCallback.clickView(0, position);
      }
    });
    holder.ivAsk.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        mCallback.clickView(1, position);
      }
    });

    return convertView;
  }

  static class ViewHolder {
    @BindView(R.id.iv_logo) ImageView ivLogo;
    @BindView(R.id.tv_title) TextView tvTitle;
    @BindView(R.id.tv_contentLeft) TextView tvContentLeft;
    @BindView(R.id.ll_expand) MyExpandableTextView llExpand;
    @BindView(R.id.tv_bottomLeft) TextView tvBottomLeft;
    @BindView(R.id.iv_attent) TextView ivAttent;
    @BindView(R.id.iv_ask) TextView ivAsk;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}

